<?php
/**
  * tframe
  *
  *
  * @copyright Copyright (c) 2012 - 2014, huxiaomin
  * @author huxiaomin    2012-6-23  
  * @since		Version 1.0
  *
  */
    
class DcacheKeyTable{

    static $all_key = 0;
    static $single_key = array();
    public $ttable = '';
    public $db = '';

    function getSingleKeyVersion($gcache_key){
        if (isset(self::$single_key[$gcache_key]) && self::$single_key[$gcache_key]){
            return self::$single_key[$gcache_key];
        }
        $gcache_key = mysql_real_escape_string($gcache_key);
        $key = 'SELECT version FROM dcache_key WHERE gcache_key='."'{$gcache_key}'";
        $TMemcache = new TMemcache();
        $cache_value = $TMemcache->get($key);
        if ($cache_value){
            self::$all_key = $cache_value;
            return $cache_value;
        }
        
        $cache_value = $this->db->GetValue($key);
        //如果为空的话，就需要自动放入数据库中
        if (empty($cache_value)){
            $sql = "INSERT INTO dcache_key (gcache_key,version,update_time,type)
            VALUES ('{$gcache_key}',1,now(),'single')";
            $this->db->Query($sql);
        }
        $TMemcache->set($key,$cache_value,0,86400);
        return $cache_value;
    
    }
    
    function getAllKeyVersion(){
        if (self::$all_key){
            return self::$all_key;
        }

        $key = 'SELECT version FROM dcache_key WHERE id=1';
        $TMemcache = new TMemcache();
        $cache_value = $TMemcache->get($key);
        if ($cache_value){
            self::$all_key = $cache_value;
            return $cache_value;
        }
        
        $cache_value = $this->db->GetValue($key);
        $TMemcache->set($key,$cache_value,0,86400);
        return $cache_value;
    }

}